﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Smallerp.Contracts;
using SmallERP.DAL;
using System.Data;
using System.Data.OleDb;
using Smallerp.Contracts.DataContracts;
using SmallERP.COM;

namespace Smallerp.Services
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class OperateDBFenPian : IOperateDBFenPian
    {
        public DataTable QueryDataTable(string inSql)
        {
            return AccessHelper.dataTable(inSql);
        }

        public int ExecuteNonQuery(string sqlstr)
        {
            return AccessHelper.ExecuteNonQuery(sqlstr);
        }
    
        public object ExecuteScalar(string sqlstr)
        {
            return AccessHelper.ExecuteScalar(sqlstr);
        }

        /// <summary>
        /// 照片分配格子
        /// </summary>
        /// <param name="inPhotoID"ID</param>
        /// <param name="OutGridName">格子名字</param>
        /// <param name="outGridInfo_Grid">刷新界面的实体</param>
        /// <returns></returns>
        public int AssinGrid(int inPhotoID, out string OutGridName, out GridInfo_GridDCT outGridInfo_Grid, out int outGridID, int inSubAreaID)
        {

            OleDbConnection oleConn = AccessHelper.OpenMyConnection();
            try
            {
                using (OleDbTransaction tran = oleConn.BeginTransaction())
                {
                    string QueryPhoto = "select * from photoinfo where photoid=" + inPhotoID;
                    DataTable tablePhoto = AccessHelper.dataTable(QueryPhoto, oleConn, tran);
                    if (tablePhoto.Rows.Count == 0)
                    {
                        OutGridName = "";
                        outGridInfo_Grid = null;
                        outGridID = 0;
                        return -1;//找不到该照片
                    }
                    else
                    {
                        int photoStatus = (int)tablePhoto.Rows[0]["Status"];
                        if (photoStatus != (int)Status.PhotoStatus.InGrid && photoStatus != (int)Status.PhotoStatus.Printed && photoStatus != (int)Status.PhotoStatus.Producted)
                        {
                            OutGridName = "";
                            outGridInfo_Grid = null;
                            outGridID = 0;
                            return -2;//照片未打印
                        }
                        else
                        {
                            int orderproductid = (int)tablePhoto.Rows[0]["orderproductid"];
                            string QueryGrid = "select * from Grid where orderid=" + orderproductid;
                            DataTable tableGrid = AccessHelper.dataTable(QueryGrid, oleConn, tran);
                            if (tableGrid.Rows.Count > 0)//已经占用格子
                            {
                                int subAreasid = (int)tableGrid.Rows[0]["SubAreaId"];
                                int gridID = (int)tableGrid.Rows[0]["gridID"];
                                if (subAreasid != inSubAreaID)
                                {
                                    OutGridName = (string)AccessHelper.ExecuteScalar("select SubAreaName from GridSubArea where SubAreaID=" + inSubAreaID, oleConn, tran);
                                    outGridInfo_Grid = null;
                                    outGridID = 0;
                                    return -5;//不属于该分区
                                }
                                else
                                {
                                    OutGridName = (string)tableGrid.Rows[0]["GridName"];

                                    if (photoStatus != (int)Status.PhotoStatus.InGrid)
                                    {
                                        int UpdatePhoto = AccessHelper.ExecuteNonQuery("update photoinfo set status=" + (int)Status.PhotoStatus.InGrid + " where photoid=" + inPhotoID, oleConn, tran);
                                        if (UpdatePhoto == 0)
                                        {
                                            tran.Rollback();
                                            OutGridName = "";
                                            outGridInfo_Grid = null;
                                            outGridID = 0;
                                            return -2;//照片未打印
                                        }
                                    }
                                    int PhotoStatusInGrid = (int)Status.PhotoStatus.InGrid;
                                    string OtherStatus = "select count(*) from photoinfo where status<>" + PhotoStatusInGrid + " and orderproductid=" + orderproductid;
                                    int otherCount = (int)AccessHelper.ExecuteScalar(OtherStatus, oleConn, tran);
                                    if (otherCount != 0)
                                    {
                                        outGridInfo_Grid = ConvertToView(gridID, orderproductid, oleConn, tran);
                                        outGridID = gridID;
                                        tran.Commit();
                                        return 0; //在格子中，未分片完成
                                    }
                                    else
                                    {
                                        string UpdateGrid = "update grid set OrderID=0,status=" + (int)Status.GridStatus.wait + " where gridid=" + gridID;
                                        int gridUp = AccessHelper.ExecuteNonQuery(UpdateGrid, oleConn, tran);
                                        if (gridUp > 0)
                                        {
                                            string updateOrder = "update orderinfo set status=" + (int)Status.OrderStatus.Complete + " where orderproductid=" + orderproductid;
                                            if (AccessHelper.ExecuteNonQuery(updateOrder, oleConn, tran) > 0)
                                            {
                                                outGridInfo_Grid = ConvertToView(gridID, orderproductid, oleConn, tran);
                                                outGridID = gridID;
                                                tran.Commit();
                                                return 1; //最后一张进格子完成
                                            }
                                            else
                                            {
                                                OutGridName = "";
                                                outGridInfo_Grid = null;
                                                outGridID = 0;
                                                tran.Rollback();
                                                return -4;//数据连接错误
                                            }
                                        }
                                        else
                                        {
                                            OutGridName = "";
                                            outGridInfo_Grid = null;
                                            outGridID = 0;
                                            tran.Rollback();
                                            return -4;//数据连接错误
                                        }
                                    }
                                }
                            }
                            else///产品未占用格子
                            {
                                //检查产品是否完成
                                string QueryOrder = "select status from orderinfo where orderproductid=" + orderproductid;
                                int Orderstatus = (int)AccessHelper.ExecuteScalar(QueryOrder, oleConn, tran);
                                if (Orderstatus == (int)Status.OrderStatus.Complete)
                                {
                                    OutGridName = "";
                                    outGridInfo_Grid = null;
                                    outGridID = 0;
                                    tran.Rollback();
                                    return 2;//产品已经已完成
                                }
                                else
                                {
                                    int GridStatusWait = (int)Status.GridStatus.wait;
                                    string QueryUseFistGrid = "select min(val(Mid(gridName,2,Len(gridName)-1))) as Num from grid  where subareaid=" + inSubAreaID + " and status=" + GridStatusWait;
                                    //DataTable t=AccessHelper.dataTable(QueryUseFistGrid, oleConn, tran);
                                    var gn = Convert.ToInt32(AccessHelper.ExecuteScalar(QueryUseFistGrid, oleConn, tran));
                                    int GridName = Convert.ToInt32(gn == null ? 0 : gn);
                                    if (GridName == 0)
                                    {
                                        OutGridName = "";
                                        outGridInfo_Grid = null;
                                        outGridID = 0;
                                        tran.Rollback();
                                        return -3;//没有可用格子 

                                    }
                                    else
                                    {
                                        string QueryPhotos = "select count(*) from photoinfo where orderproductid=" + orderproductid;
                                        if ((int)AccessHelper.ExecuteScalar(QueryPhotos, oleConn, tran) == 1)
                                        {

                                            string updatephoto = "update  photoinfo set status=" + (int)Status.PhotoStatus.InGrid + " where photoid=" + inPhotoID;
                                            if (AccessHelper.ExecuteNonQuery(updatephoto, oleConn, tran) != 0)
                                            {
                                                string updateOrder = "update orderinfo set status=" + (int)Status.OrderStatus.Complete + " where orderproductid=" + orderproductid;
                                                if (AccessHelper.ExecuteNonQuery(updateOrder, oleConn, tran) != 0)
                                                {
                                                    OutGridName = "A单张";
                                                    outGridInfo_Grid = ConvertToView(0, orderproductid, oleConn, tran);
                                                    outGridID = 0;
                                                    tran.Commit();
                                                    return 3;//单张直接完成不需要分配格子
                                                }
                                                else
                                                {
                                                    OutGridName = "";
                                                    outGridInfo_Grid = null;
                                                    outGridID = 0;
                                                    tran.Rollback();
                                                    return -4;//数据连接错误
                                                }

                                            }
                                            else
                                            {
                                                OutGridName = "";
                                                outGridInfo_Grid = null;
                                                outGridID = 0;
                                                tran.Rollback();
                                                return -4;//数据连接错误
                                            }

                                        }
                                        else//多张第一次进格子
                                        {
                                            string queryGrid = "select * from grid where val(Mid(gridName,2,Len(gridName)-1))=" + GridName + " and SubAreaId=" + inSubAreaID;
                                            DataTable tableNewGrid = AccessHelper.dataTable(queryGrid, oleConn, tran);
                                            OutGridName = (string)tableNewGrid.Rows[0]["gridname"];
                                            int gridid = (int)tableNewGrid.Rows[0]["gridid"];
                                            string updateGrid = "update grid set orderid=" + orderproductid + ",status=" + (int)Status.GridStatus.used + " where gridid=" + gridid;
                                            if (AccessHelper.ExecuteNonQuery(updateGrid, oleConn, tran) > 0)
                                            {
                                                string updatePhoto = "update photoinfo set status=" + (int)Status.PhotoStatus.InGrid + " where photoid= " + inPhotoID;
                                                if (AccessHelper.ExecuteNonQuery(updatePhoto, oleConn, tran) > 0)
                                                {
                                                    outGridInfo_Grid = ConvertToView(gridid, orderproductid, oleConn, tran);
                                                    outGridID = gridid;
                                                    tran.Commit();
                                                    return 4;//多张新分配格子 
                                                }
                                                else
                                                {
                                                    OutGridName = "";
                                                    outGridInfo_Grid = null;
                                                    outGridID = 0;
                                                    tran.Rollback();
                                                    return -4;//数据连接错误
                                                }
                                            }
                                            else
                                            {
                                                OutGridName = "";
                                                outGridInfo_Grid = null;
                                                outGridID = 0;
                                                tran.Rollback();
                                                return -4;//数据连接错误
                                            }

                                        }
                                    }

                                }

                            }

                        }

                    }

                }
            }
            finally
            {
                AccessHelper.CloseMyConnection(oleConn);
            }
        }

        private GridInfo_GridDCT ConvertToView(int gridID, int orderProductid, OleDbConnection conn, OleDbTransaction tran)
        {
            GridInfo_GridDCT gridInfoGrid = new GridInfo_GridDCT();
            if (orderProductid != null && orderProductid != 0)
            {
                string QueryOrderinfo = "select * from orderinfo,papertype,paperface where orderinfo.papertypeid=papertype.papertypeid and paperface.paperfaceid=orderinfo.paperfaceid and orderproductid=" + orderProductid;
                DataTable tableOrder = AccessHelper.dataTable(QueryOrderinfo, conn, tran);
                OrderViewDCT product = new OrderViewDCT() { CompleteTime = Convert.ToDateTime(tableOrder.Rows[0]["CompleteTime"]), OrderName = Convert.ToString(tableOrder.Rows[0]["OrderName"]), OrderPath = Convert.ToString(tableOrder.Rows[0]["OrderPath"]), OrderProductId = Convert.ToInt32(tableOrder.Rows[0]["OrderProductId"]), Priority = Convert.ToInt32(tableOrder.Rows[0]["Priority"]), ProductNum = Convert.ToInt32(tableOrder.Rows[0]["ProductNum"]), Remark = Convert.ToString(tableOrder.Rows[0]["Remark"]), StartTime = Convert.ToDateTime(tableOrder.Rows[0]["StartTime"]), Status = Convert.ToInt32(tableOrder.Rows[0]["Status"]) };
                gridInfoGrid.OrderInfo = product;
                List<PhotoDCT> listPhotoInfo = new List<PhotoDCT>();
                string QueryPhoto = "select * from photoinfo where orderproductid=" + Convert.ToInt32(tableOrder.Rows[0]["OrderProductId"]);
                DataTable tablePhoto = AccessHelper.dataTable(QueryPhoto, conn, tran);
                PhotoDCT info;
                for (int j = 0; j < tablePhoto.Rows.Count; j++)
                {
                    info = new PhotoDCT()
                    {
                        Height = Convert.ToInt32(tablePhoto.Rows[j]["Height"]),
                        Width = Convert.ToInt32(tablePhoto.Rows[j]["Width"]),
                        IsReback = Convert.ToBoolean(tablePhoto.Rows[j]["IsReback"]),
                        OrderProductId = Convert.ToInt32(tableOrder.Rows[0]["OrderProductId"]),
                        PhotoId = Convert.ToInt32(tablePhoto.Rows[j]["PhotoId"]),
                        PhotoName = Convert.ToString(tablePhoto.Rows[j]["PhotoName"]),
                        PicPath = Convert.ToString(tablePhoto.Rows[j]["PicPath"]),
                        PixHeight = Convert.ToInt32(tablePhoto.Rows[j]["PixHeight"]),
                        PixWidth = Convert.ToInt32(tablePhoto.Rows[j]["PixWidth"]),
                        Status = Convert.ToInt32(tablePhoto.Rows[j]["Status"]),
                        PrinterId = Convert.ToInt32(tablePhoto.Rows[j]["PrinterId"])

                    };
                    if (info.Height < info.Width)
                    {
                        info.SizeInch = info.Height + "X" + info.Width;
                        info.SizeCM = info.Height * 2.54 + "X" + info.Width * 2.54;
                    }
                    else
                    {
                        info.SizeInch = info.Width + "X" + info.Height;
                        info.SizeCM = info.Width * 2.54 + "X" + info.Width * 2.54;
                    }
                    info.PaperFace = Convert.ToString(tableOrder.Rows[0]["PaperFaceName"]);
                    info.PaperType = Convert.ToString(tableOrder.Rows[0]["PaperTypeName"]);
                    if (info.PrinterId != 0)
                    {
                        info.PrinterName = (string)AccessHelper.ExecuteScalar("select Pname from printer where printerid=" + info.PrinterId);
                    }
                    listPhotoInfo.Add(info);

                }
                product.PhotoS = listPhotoInfo;
                // Grid_InfoDCT gridInfo = new Grid_InfoDCT() { GridId = (int)GridTable.Rows[i]["GridId"], FirstEnterTime = (DateTime)GridTable.Rows[i]["FirstEnterTime"], GridName = (string)GridTable.Rows[i]["GridName"], OrderID = (int)GridTable.Rows[i]["OrderID"], Status = (int)GridTable.Rows[i]["Status"] };
                //gridInfoGrid.GridBaseInfo = gridInfo;
            }
            else
            {
                gridInfoGrid.OrderInfo = null;
            }

            if (gridID != null && gridID != 0)
            {
                string queryGrid = "select *  from grid  where gridid=" + gridID;
                DataTable GridTable = AccessHelper.dataTable(queryGrid, conn, tran);
                if (GridTable.Rows.Count > 0)
                {
                    Grid_InfoDCT gridInfo = new Grid_InfoDCT() { GridId = (int)GridTable.Rows[0]["GridId"], FirstEnterTime = (DateTime)GridTable.Rows[0]["FirstEnterTime"], GridName = (string)GridTable.Rows[0]["GridName"], OrderID = (int)GridTable.Rows[0]["OrderID"], Status = (int)GridTable.Rows[0]["Status"] };
                    gridInfoGrid.GridBaseInfo = gridInfo;
                }
                else
                {
                    gridInfoGrid.GridBaseInfo = null;
                }

            }
            return gridInfoGrid;
        }
        /// <summary>
        /// 强制打印格子
        /// </summary>
        /// <param name="inGGD"></param>
        /// <returns></returns>
        public int ForcePrintGrid(GridInfo_GridDCT inGGD)
        {
            int rtn = 0;
            OleDbConnection oleConn = AccessHelper.OpenMyConnection();
            using (OleDbTransaction tran = oleConn.BeginTransaction())
            {
                string checkPhoto = "select count(*) from photoinfo where orderproductid=" + inGGD.OrderInfo.OrderProductId + " and status=" + (int)Status.PhotoStatus.WaitPB;
                if ((int)AccessHelper.ExecuteScalar(checkPhoto, oleConn, tran) > 0)
                {
                    rtn = 1;///有未拼版照片不能强制打印
                    return rtn;
                }
                string updateOrder = "update orderinfo set status=" + (int)Status.OrderStatus.Complete + " where orderproductid=" + inGGD.OrderInfo.OrderProductId;
                if (AccessHelper.ExecuteNonQuery(updateOrder, oleConn, tran) > 0)
                {
                    string updatePhotoStatus = "update photoinfo set status=" + (int)Status.PhotoStatus.InGrid + " where orderproductid=" + inGGD.OrderInfo.OrderProductId;
                    if (AccessHelper.ExecuteNonQuery(updatePhotoStatus, oleConn, tran) > 0)
                    {
                        string UpdateGrid = "update grid set OrderID=0,status=" + (int)Status.GridStatus.wait + ",FirstEnterTime='" + "2000-01-01" + "' where gridid=" + inGGD.GridBaseInfo.GridId;
                        if (AccessHelper.ExecuteNonQuery(UpdateGrid, oleConn, tran) > 0)
                        {
                            rtn = 0;
                            tran.Commit();
                        }
                        else
                        {
                            rtn = -1;
                            tran.Rollback();
                        }

                    }
                    else
                    {
                        rtn = -1;
                        tran.Rollback();
                    }
                }
                else
                {
                    rtn = -1;
                    tran.Rollback();
                }
            }
            AccessHelper.CloseMyConnection(oleConn);
            return rtn;
        }
        /// <summary>
        /// 清空格子
        /// </summary>
        /// <param name="inGrid"></param>
        /// <returns></returns>
        public int ClearSelectedGrid(GridInfo_GridDCT inGGD)
        {
            int rtn = 0;
            OleDbConnection oleConn = AccessHelper.OpenMyConnection();
            using (OleDbTransaction tran = oleConn.BeginTransaction())
            {
                string updateOrder = "update orderinfo set status=" + (int)Status.OrderStatus.Doing + " where orderproductid=" + inGGD.OrderInfo.OrderProductId;
                if (AccessHelper.ExecuteNonQuery(updateOrder, oleConn, tran) > 0)
                {
                    string updatePhotoStatus = "update photoinfo set status=" + (int)Status.PhotoStatus.Printed + " where orderproductid=" + inGGD.OrderInfo.OrderProductId + " and status=" + (int)Status.PhotoStatus.InGrid;
                    if (AccessHelper.ExecuteNonQuery(updatePhotoStatus, oleConn, tran) > 0)
                    {
                        string UpdateGrid = "update grid set OrderID=0,status=" + (int)Status.GridStatus.wait + ",FirstEnterTime='" + "2000-01-01" + "' where gridid=" + inGGD.GridBaseInfo.GridId;
                        if (AccessHelper.ExecuteNonQuery(UpdateGrid, oleConn, tran) > 0)
                        {
                            rtn = 0;
                            tran.Commit();
                        }
                        else
                        {
                            rtn = -1;
                            tran.Rollback();
                        }

                    }
                    else
                    {
                        rtn = -1;
                        tran.Rollback();
                    }
                }
                else
                {
                    rtn = -1;
                    tran.Rollback();
                }
            }
            AccessHelper.CloseMyConnection(oleConn);
            return rtn;
        }
        /// <summary>
        /// 清柜
        /// </summary>
        /// <param name="infoGrids"></param>
        /// <param name="inNum"></param>
        /// <returns></returns>
        public int PriorityPhoto(List<GridInfo_GridDCT> infoGrids, int inNum)
        {
            int intVal = 0;
            OleDbConnection conn = AccessHelper.OpenMyConnection();
            using (OleDbTransaction tran = conn.BeginTransaction())
            {
                foreach (GridInfo_GridDCT grid in infoGrids)
                {
                    //一个格子就是一个产品

                    List<PhotoDCT> photoList =
                        grid.OrderInfo.PhotoS.Where(
                            p =>
                            p.Status != (int)Status.PhotoStatus.Printed &&
                            p.Status != (int)Status.PhotoStatus.InGrid).ToList();
                    if (inNum == 0)
                    {
                        //等于0为不限制
                        string updatePhoto = "update photoinfo set IsClearBox=" + Convert.ToInt32(true) + " where orderproductid=" + grid.OrderInfo.OrderProductId + " and  status<>" + (int)Status.PhotoStatus.Printed + " and status<>" + (int)Status.PhotoStatus.InGrid;
                        if (AccessHelper.ExecuteNonQuery(updatePhoto, conn, tran) > 0)
                        {
                            string updateBoard = "update board set PrintPriority=12 where status<>" + (int)Status.EnumBoardStatus.Printed + " and boardid in (select distinct board.boardid from board,picmakeup where board.boardid=picmakeup.boardid and picmakeup.photoid in (select photoid from photoinfo  where orderproductid=" + grid.OrderInfo.OrderProductId + " and status<>" + (int)Status.PhotoStatus.Printed + " and status<>" + (int)Status.PhotoStatus.InGrid + "))";
                            if (AccessHelper.ExecuteNonQuery(updateBoard, conn, tran) > 0)
                            {
                                intVal = 0;
                            }
                            else
                            {
                                //intVal = -1;
                                //break;
                            }

                        }
                    }
                    else
                    {
                        if (photoList.Count > inNum)
                        {
                            continue;//大于限制条件的不做处理
                        }
                        else
                        {
                            string updatePhoto = "update photoinfo set IsClearBox=" + Convert.ToInt32(true) + " where orderproductid=" + grid.OrderInfo.OrderProductId + " and  status<>" + (int)Status.PhotoStatus.Printed + " and status<>" + (int)Status.PhotoStatus.InGrid;
                            if (AccessHelper.ExecuteNonQuery(updatePhoto, conn, tran) > 0)
                            {
                                string updateBoard = "update board set PrintPriority=12 where status<>" + (int)Status.EnumBoardStatus.Printed + " and boardid in (select distinct boardid from baord,picmakeup where board.baardid=picmakeup.boardid and picmakeup.photoid in (select photoid from photoinfo  where orderproductid=" + grid.OrderInfo.OrderProductId + " and status<>" + (int)Status.PhotoStatus.Printed + " and status<>" + (int)Status.PhotoStatus.InGrid + "))";
                                if (AccessHelper.ExecuteNonQuery(updateBoard, conn, tran) > 0)
                                {
                                    intVal = 0;
                                }
                                else
                                {
                                    //intVal = -1;
                                    //break;
                                }

                            }


                        }
                    }


                }
                if (intVal == 0)
                {
                    tran.Commit();
                }
                else
                {
                    tran.Rollback();
                }
            }

            AccessHelper.CloseMyConnection(conn);
            return intVal;
        }
        public int CreateInnerReturn(List<InnerReturn_InfoDCT> CurrPhotoList, out Dictionary<int, int> outPhotoDict)
        {
            //List<PaperWasteRecord_info> listRecord = new List<PaperWasteRecord_info>();
            outPhotoDict = new Dictionary<int, int>();
            OleDbConnection conn = AccessHelper.OpenMyConnection();
            int rtnVal = 0;
            using (OleDbTransaction tran = conn.BeginTransaction())
            {
                List<string> userList = new List<string>();
                Dictionary<int, string> WasteTypeList = new Dictionary<int, string>();
                string QueryName = "select * from Users ";
                DataTable tableUser = AccessHelper.dataTable(QueryName);
                for (int i = 0; i < tableUser.Rows.Count; i++)
                {
                    userList.Add(tableUser.Rows[i]["username"].ToString());
                }
                string QueryWasteType = "select * from PaperWasteType ";
                DataTable TableWasteType = AccessHelper.dataTable(QueryWasteType);
                for (int i = 0; i < TableWasteType.Rows.Count; i++)
                {
                    WasteTypeList.Add((int)TableWasteType.Rows[i]["id"], TableWasteType.Rows[i]["PaperOperationTypeName"].ToString());
                }
                foreach (var g in CurrPhotoList.GroupBy(p => p.OrderProductId))
                {
                    foreach (InnerReturn_InfoDCT viewPhoto in g)
                    {
                        if (!userList.Contains(viewPhoto.CreateUserName))
                        {

                            string insertUser = "insert into users (username) values('" + viewPhoto.CreateUserName + "')";
                            if (AccessHelper.ExecuteNonQuery(insertUser, conn, tran) > 0)
                            {
                                userList.Add(viewPhoto.CreateUserName);
                            }
                        }
                        if (!userList.Contains(viewPhoto.ResponseUserName))
                        {
                            string insertUser = "insert into users (username) values('" + viewPhoto.CreateUserName + "')";
                            if (AccessHelper.ExecuteNonQuery(insertUser, conn, tran) > 0)
                            {
                                userList.Add(viewPhoto.CreateUserName);
                            }
                        }
                        if (!WasteTypeList.Values.Contains(viewPhoto.Remark))
                        {

                            string insertWasteType = "insert into PaperWasteType (PaperOperationTypeName) values(" + viewPhoto.Remark + ")";
                            if (AccessHelper.ExecuteNonQuery(insertWasteType, conn, tran) > 0)
                            {
                                string QuerynewWasteType = "select id from PaperWasteType where PaperOperationTypeName=" + viewPhoto.Remark;
                                WasteTypeList.Add((int)AccessHelper.ExecuteScalar(QuerynewWasteType, conn, tran), viewPhoto.Remark);
                            }
                        }
                        //                string QueryInnerReturn = "select orderinfo.OrderProductId,photoid,width,heigth,picpath,orderName,photoname,photostatus from orderinfo,photoinfo where orderinfo.orderproductid=photoinfo.orderproductid where photoid=" + viewPhoto.PhotoId;
                        //DataTable tablePhoto = AccessHelper.dataTable(QueryInnerReturn);

                        //string insertSql = "insert into InnerReturn(OrderproductID,PhotoId,Width,Heigth,Width,PhotoAreas,PicPath,CreateTime,Remark,ResponMan,CreateMan) values("
                        //    + (int)tablePhoto.Rows[0]["OrderProductId"] + ","
                        //    + (int)tablePhoto.Rows[0]["PhotoId"] + ","
                        //    + (float)tablePhoto.Rows[0]["Width"] + ","
                        //    + (float)tablePhoto.Rows[0]["Height"] + ","
                        //    + (float)tablePhoto.Rows[0]["Width"] * (float)tablePhoto.Rows[0]["Height"] * 2.54 * 2.54 / 10000+","
                        //    + "'"+(string)tablePhoto.Rows[0]["picpath"]+"',"
                        //    +"'"+DateTime.Now+"',"
                        //    +"'"+viewPhoto.Remark+"',"
                        //    +"'"+viewPhoto.ResponseUserName+"',"
                        //    +"'"+viewPhoto.CreateUserName+"')";
                        string insertInnerreturn = "insert into InnerReturn(OrderproductID,PhotoId,Width,Heigth,PhotoAreas,PicPath,CreateTime,Remark,ResponMan,CreateMan) values("
                            + viewPhoto.OrderProductId + ","
                            + viewPhoto.PhotoId + ","
                            + viewPhoto.Width + ","
                            + viewPhoto.Height + ","
                            + viewPhoto.Width * viewPhoto.Height * 2.54 * 2.54 / 10000 + ","
                            + "'" + viewPhoto.PicPath.Replace("'","''") + "',"
                            + "'" + DateTime.Now + "',"
                            + "'" + viewPhoto.Remark + "',"
                            + "'" + viewPhoto.ResponseUserName + "',"
                            + "'" + viewPhoto.CreateUserName + "')";
                        int insertInnerInt = AccessHelper.ExecuteNonQuery(insertInnerreturn, conn, tran);
                        if (insertInnerInt > 0)
                        {
                            //order.Status = (int)Status.OrderStatus.Doing;
                            //photo.Status = (int)Status.PhotoStatus.WaitPB;

                            string updateOrderSql = "update orderinfo set status=" + (int)Status.OrderStatus.Doing + " where orderproductid=" + viewPhoto.OrderProductId;
                            int updateOrder = AccessHelper.ExecuteNonQuery(updateOrderSql, conn, tran);
                            string updatephotoSql = "update photoinfo set status=" + (int)Status.PhotoStatus.WaitPB + " where photoid=" + viewPhoto.PhotoId;
                            int updatephoto = AccessHelper.ExecuteNonQuery(updatephotoSql, conn, tran);
                            DateTime timeNow = DateTime.Now;
                            string QueryPaperid = "select PrintPaperId from board where boardid=(select boardid from picmakeup where photoid=" + viewPhoto.PhotoId + ")";
                            int paperid = (int)AccessHelper.ExecuteScalar(QueryPaperid, conn, tran);
                            string insertPaperWasteRecords = "insert into PaperWasteRecords(PaperWasteTypeId,PrintPaperId,Length,Width,CreateTime,Status,Remark) values("
                                + WasteTypeList.FirstOrDefault(p => p.Value == viewPhoto.Remark).Key + ","
                                + insertInnerInt + ","
                                + viewPhoto.Height * 2.54 / 100 + ","
                                + viewPhoto.Width * 2.54 / 100 + ","
                                + "'" + timeNow + "',"
                                + (int)Status.WasterPaperStatus.unSure + ","
                                + "'" + viewPhoto.Remark + "')";
                            if (AccessHelper.ExecuteNonQuery(insertPaperWasteRecords, conn, tran) > 0)
                            {
                                int wId = (int)AccessHelper.ExecuteScalar("select Id from PaperWasteRecords where createtime=#" + timeNow + "#", conn, tran);
                                outPhotoDict.Add(viewPhoto.PhotoId, wId);
                                rtnVal = 0;
                            }
                            else
                            {
                                rtnVal = -1;
                                goto bc;
                            }

                        }
                        else
                        {
                            rtnVal = -1;
                            goto bc;
                        }
                    }







                }
            bc:
                if (rtnVal == 0)
                {
                    tran.Commit();
                }
                else
                {
                    tran.Rollback();
                }
            }

            return rtnVal;
        }
    }
}
